<?php
namespace app\modules\pay\provider;

use app\utils\HttpUtil;
use yii\base\UserException;

class PayFactory {

    const PAYMENT_URL = "https://pays.jk.pays-world.com/pay/api/gateway";

    public static $payment = [
        '2'=>WechatProvider::class,
        '3'=>AlipayProvider::class,
    ];

    //获取支付代码
    public static function getPayCode($payment,$order_no,$total_amount,$body,$openid,$config){
        if(!array_key_exists($payment,self::$payment)){
            throw new UserException(\Yii::t("app","支付方式不支持"));
        }

        $params = self::$payment[$payment]::getPayCode($order_no,$total_amount,$body,$openid,$config);
        return self::request($params);
    }

    //退款
    public static function getRefundCode($order_no,$refund_no,$refund_amount,$pay_channel,$config){
        $params = RefundProvider::getRefundCode($order_no,$refund_no,$refund_amount,$pay_channel,$config);
        return self::request($params);
    }

    //发起请求
    protected static function request($params){
        $result = HttpUtil::request(self::PAYMENT_URL,HttpUtil::METHOD_POST,$params,HttpUtil::FORMAT_JSON);
        if($result['code'] != "000000"){
            throw new UserException($result['msg']);
        }
        return $result;
    }
}